/*
 * Created using SharpDevelop.
 * User: Steve
 * Date: 11/11/2006
 * Time: 9:00 PM
 * 
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using Mokey.ThunderCards.Cards;
using Mokey.ThunderCards.Settings;

namespace Mokey.ThunderCards
{
	/// <summary>
	/// Description of PopUpForm.
	/// </summary>
	public partial class PopUpForm
	{
		public PopUpForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		}
		
		private ThunderCard _loadedCard;
        private const int POSITION_PADDING = 5;
        private const int TEXTBOX_PADDING = 10;
		
		#region Public Methods

        public void LoadCard(ThunderCard cardToLoad, SoundStatusEnum soundBehaviour, bool autoShowQuestion, bool autoShowAnswer,
            string mediaFolderPath)
		{
            _loadedCard = cardToLoad;

            SetDefaults();

            if (!string.IsNullOrEmpty(_loadedCard.Question))
            {
                SetTextBox(QuestionTextBox, _loadedCard.Question);
            }

            if (!string.IsNullOrEmpty(_loadedCard.Answer))
            {
                SetTextBox(AnswerTextBox, _loadedCard.Answer);
            }

            if (!string.IsNullOrEmpty(_loadedCard.QuestionImageFileName))
            {
                SetImage(QuestionPictureBox, mediaFolderPath, _loadedCard.QuestionImageFileName);
            }

            if (!string.IsNullOrEmpty(_loadedCard.AnswerImageFileName))
            {
                SetImage(AnswerPictureBox, mediaFolderPath, _loadedCard.AnswerImageFileName);
            }

            if (!string.IsNullOrEmpty(_loadedCard.SoundFileName))
            {
                PlaySoundButton.Visible = true;
                if (soundBehaviour == SoundStatusEnum.AutoPlay)
                {
                    PlaySound();
                }
            }
			
			if (autoShowQuestion)
			{
                ShowQuestion();
			}
			
			if (autoShowAnswer)
			{
				ShowAnswer();
			}
		}

		public void PlaySound()
		{
			//TODO: PlaySound
		}
		
		public void ShowQuestion()
		{
            QuestionPanel.Visible = true;
            ShowQuestionButton.Visible = false;
		}

        public void ShowAnswer()
		{
            AnswerPanel.Visible = true;
            ShowAnswerButton.Visible = false;
		}

        public void SetSizeAndPosition(Size size)
        {
            Size = size;
            Location = new Point(Screen.PrimaryScreen.WorkingArea.Right - Size.Width - POSITION_PADDING,
                Screen.PrimaryScreen.WorkingArea.Bottom - Size.Height - POSITION_PADDING);
            StartPosition = FormStartPosition.Manual;
        }
		
		#endregion

        #region  Private Methods

        private void SetImage(PictureBox pictureBoxToSet, string mediaFolderPath, string imageFileName)
        {
            if (imageFileName.Contains("\\"))
            {
                pictureBoxToSet.ImageLocation = imageFileName;
            }
            else
            {
                pictureBoxToSet.ImageLocation = string.Format("{0}\\{1}", mediaFolderPath, imageFileName);
            }
            pictureBoxToSet.Visible = true;
        }

        private void SetTextBox(TextBox textBoxToSet, string text)
        {
            textBoxToSet.Text = text;
            textBoxToSet.Visible = true;
            textBoxToSet.SelectionStart = 0;
            textBoxToSet.SelectionLength = 0;
            SetScrollbarDisplay(textBoxToSet);
        }

        private void SetDefaults()
        {
            ShowQuestionButton.Visible = true;
            QuestionPanel.Visible = false;
            QuestionTextBox.Visible = false;
            QuestionPictureBox.Visible = false;

            ShowAnswerButton.Visible = true;
            AnswerPanel.Visible = false;
            AnswerTextBox.Visible = false;
            AnswerPictureBox.Visible = false;
        }

        private void SetScrollbarDisplay(TextBox textBox)
        {
            Graphics g = textBox.CreateGraphics();
            if (g.MeasureString(textBox.Text, textBox.Font, textBox.Width).Height >= textBox.Height - TEXTBOX_PADDING)
            {
                textBox.ScrollBars = ScrollBars.Vertical;
            }
            else
            {
                textBox.ScrollBars = ScrollBars.None;
            }
        }
		
        #endregion

        #region Event Handlers

        private void ShowQuestionButtonClick(object sender, System.EventArgs e)
		{
            ShowQuestion();
		}

        private void ShowAnswerButtonClick(object sender, System.EventArgs e)
		{
            ShowAnswer();
		}

        private void PlaySoundButtonClick(object sender, System.EventArgs e)
		{
			PlaySound();
		}
		
        private void PopUpForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Hide();
            }
        }

        #endregion

        public event EventHandler UserInteracted;
        public event EventHandler UserFinishedInteraction;

        private void PopUpForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (UserInteracted != null)
            {
                UserInteracted(this, new EventArgs());
            }
        }

        private void PopUpForm_MouseLeave(object sender, EventArgs e)
        {
            if (UserFinishedInteraction != null)
            {
                UserFinishedInteraction(this, new EventArgs());
            }
        }
	}
}
